<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 求和 -->
    <input type="text" value="1,2,3"> <span>输入数字求和，数字之间用半角','号分隔</span> <br><br>
    <button>求和</button>
    <p class="he"> </p>
</body>

</html>
<script>
    var input = document.querySelector("input")
    var button = document.querySelector("button")
    var he = document.querySelector(".he")
    button.onclick = function (){
        var obj = input.value    //先将input的value数据赋值给obj
        console.log(obj)         
        var arr = obj.split(",") //截掉逗号在赋给arr
        var num = 0              //定义计算总和的初始值
        for (var i = 0; i < arr.length; i++) { 
            num += parseInt(arr[i]) 
        }
        he.innerHTML = num

    }
</script>
<style>

</style>